<!-- components/procurement/ProductImportExport.vue -->
<template>
  <div>
    <!-- 导入对话框 -->
    <el-dialog
      v-model="importDialogVisible"
      title="导入商品"
      width="500px"
    >
      <el-form :model="importForm" label-width="100px">
        <el-form-item label="选择文件">
          <el-upload
            ref="uploadRef"
            class="upload-demo"
            drag
            :auto-upload="false"
            :on-change="handleFileChange"
            :limit="1"
            accept=".xlsx,.xls,.csv"
          >
            <el-icon class="el-icon--upload"><upload-filled /></el-icon>
            <div class="el-upload__text">
              将文件拖到此处，或<em>点击上传</em>
            </div>
            <template #tip>
              <div class="el-upload__tip">
                请上传Excel或CSV格式文件，文件大小不超过10MB
              </div>
            </template>
          </el-upload>
        </el-form-item>
        
        <el-form-item label="导入模式">
          <el-radio-group v-model="importForm.mode">
            <el-radio value="overwrite">覆盖已有数据</el-radio>
            <el-radio value="skip">跳过已有数据</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      
      <template #footer>
        <el-button @click="importDialogVisible = false">取消</el-button>
        <el-button 
          type="primary" 
          @click="handleImport" 
          :loading="importLoading"
          :disabled="!importFile"
        >
          导入
        </el-button>
      </template>
    </el-dialog>
    
    <!-- 导出对话框 -->
    <el-dialog
      v-model="exportDialogVisible"
      title="导出商品"
      width="500px"
    >
      <el-form :model="exportForm" label-width="100px">
        <el-form-item label="导出范围">
          <el-radio-group v-model="exportForm.range">
            <el-radio value="current">当前页</el-radio>
            <el-radio value="selected">已选数据</el-radio>
            <el-radio value="all">全部数据</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item label="文件格式">
          <el-radio-group v-model="exportForm.format">
            <el-radio value="xlsx">Excel (.xlsx)</el-radio>
            <el-radio value="csv">CSV (.csv)</el-radio>
            <el-radio value="pdf">PDF (.pdf)</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item label="导出字段">
          <el-checkbox-group v-model="exportForm.fields">
            <el-checkbox 
              v-for="field in exportFields" 
              :key="field.value" 
              :label="field.value"
            >
              {{ field.label }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      
      <template #footer>
        <el-button @click="exportDialogVisible = false">取消</el-button>
        <el-button 
          type="primary" 
          @click="handleExport" 
          :loading="exportLoading"
        >
          导出
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, defineProps, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import { UploadFilled } from '@element-plus/icons-vue'
import type { UploadInstance } from 'element-plus'

interface ImportForm {
  mode: 'overwrite' | 'skip'
}

interface ExportForm {
  range: 'current' | 'selected' | 'all'
  format: 'xlsx' | 'csv' | 'pdf'
  fields: string[]
}

interface ExportField {
  label: string
  value: string
}

const props = defineProps<{
  selectedItems: any[]
}>()

const emit = defineEmits<{
  (e: 'import', file: File, mode: string): void
  (e: 'export', options: ExportForm): void
}>()

const uploadRef = ref<UploadInstance>()
const importDialogVisible = ref(false)
const exportDialogVisible = ref(false)
const importLoading = ref(false)
const exportLoading = ref(false)
const importFile = ref<File | null>(null)

const importForm = ref<ImportForm>({
  mode: 'skip'
})

const exportForm = ref<ExportForm>({
  range: 'current',
  format: 'xlsx',
  fields: [
    'name',
    'category',
    'specification',
    'unit',
    'unitPrice',
    'stockQuantity'
  ]
})

const exportFields = ref<ExportField[]>([
  { label: '商品名称', value: 'name' },
  { label: '分类', value: 'category' },
  { label: '规格', value: 'specification' },
  { label: '单位', value: 'unit' },
  { label: '单价', value: 'unitPrice' },
  { label: '库存数量', value: 'stockQuantity' },
  { label: '备注', value: 'remark' }
])

const showImportDialog = () => {
  importDialogVisible.value = true
  importFile.value = null
  importForm.value.mode = 'skip'
}

const showExportDialog = () => {
  exportDialogVisible.value = true
  // 如果有选中项，默认选择已选数据
  if (props.selectedItems.length > 0) {
    exportForm.value.range = 'selected'
  }
}

const handleFileChange = (uploadFile: any) => {
  importFile.value = uploadFile.raw
}

const handleImport = async () => {
  if (!importFile.value) {
    ElMessage.warning('请先选择文件')
    return
  }
  
  importLoading.value = true
  try {
    emit('import', importFile.value, importForm.value.mode)
    importDialogVisible.value = false
    ElMessage.success('导入成功')
  } catch (error) {
    ElMessage.error('导入失败')
  } finally {
    importLoading.value = false
  }
}

const handleExport = async () => {
  exportLoading.value = true
  try {
    emit('export', exportForm.value)
    exportDialogVisible.value = false
    ElMessage.success('导出任务已提交')
  } catch (error) {
    ElMessage.error('导出失败')
  } finally {
    exportLoading.value = false
  }
}

defineExpose({
  showImportDialog,
  showExportDialog
})
</script>

<style scoped>
.upload-demo {
  width: 100%;
}
</style>